﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
namespace NLBS.Web
{
   public class RequestWrapper
    {
       static public string ApplicationPath
       {

           get
           {
               string applicationPath = "/";

               if (HttpContext.Current != null)
                   applicationPath = HttpContext.Current.Request.ApplicationPath;

               // Are we in an application?
               //
               if (applicationPath == "/")
               {
                   return string.Empty;
               }
               else
               {
                   return applicationPath;
               }
           }
       }

       static public string IP
       {
           get
           {
               return HttpContext.Current.Request.UserHostAddress;
           }
       }

       /// <summary>
       /// convert query to int
       /// </summary>
       /// <param name="strName"></param>
       /// <returns></returns>
       public static int GetQueryInt(string strName)
       {
           int query=-1;
           if (HttpContext.Current.Request.QueryString[strName] != null)
           {
               try
               {
                   query = Convert.ToInt32(HttpContext.Current.Request.QueryString[strName]);
               }
               catch (Exception e)
               {

                   throw new Exception("参数不正确");
                   throw e;
               }

           }
           return query;
       }

       public static string GetQueryString(string strName)
       {
           if (HttpContext.Current.Request.QueryString[strName] == null)
           {
               return "";
           }
           return HttpContext.Current.Request.QueryString[strName];
       }

       public static string GetFormString(string strName)
       {
           if (HttpContext.Current.Request.Form[strName] == null)
           {
               return "";
           }
           return HttpContext.Current.Request.Form[strName];
       }

       public static bool GetFormbool(string strName)
       {
           if (HttpContext.Current.Request.Form[strName] == null)
           {
               return false;
           }
           return Convert.ToBoolean(HttpContext.Current.Request.Form[strName]);
       }

       public static string GetFormString(HtmlInputControl input)
       {
           if (!String.IsNullOrEmpty(HttpContext.Current.Request.Form[input.ID]))
           {
               return HttpContext.Current.Request.Form[input.ID];
           }
           else
           {
               return input.Value;
           }
       }

       public static bool GetCheckForm(HtmlInputCheckBox input)
       {
           if (!String.IsNullOrEmpty(HttpContext.Current.Request.Form[input.ID]))
           {
               return true;
           }
           else
           {
               return false;
           }
           return input.Checked;
       }

    }
}
